package com.task.core.dto.response;

import com.alibaba.fastjson2.JSONArray;
import com.task.core.domain.EngineInfo;
import com.task.core.domain.SysFileInfo;
import org.springframework.util.ObjectUtils;

import cn.hutool.json.JSONObject;

import com.alibaba.fastjson2.JSON;
import com.task.common.domain.info.CountryInfo;
import com.task.core.domain.DataOperationInfo;
import com.task.core.domain.MissionInfo;
import com.task.core.domain.info.ClientCountryInfo;
import com.task.core.domain.info.ClientDeviceFraudInfo;
import com.task.core.domain.info.ClientEngineInfo;
import com.task.core.domain.info.ClientFlowFilterInfo;
import com.task.core.domain.info.ClientIpFraudInfo;
import com.task.core.domain.info.ClientMissionInfo;
import com.task.core.domain.info.ClientPluginInfo;
import com.task.core.domain.info.ClientProxyInfo;
import com.task.core.domain.info.ClientScriptFileInfo;

import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 迪迦.
 * @date 2024/11/13 17:06
 */
@Data
public class MissionOperationResponseDTO {

  private ClientMissionInfo missionInfo;
  private String missionSessionCode;

  public MissionOperationResponseDTO() {
  }

  public MissionOperationResponseDTO(MissionInfo missionInfo, String missionSessionCode, CountryInfo countryInfo) {
    this.missionInfo = changeToClientMissionInfo(missionInfo, countryInfo);
    this.missionSessionCode = missionSessionCode;
  }

  private ClientMissionInfo changeToClientMissionInfo(MissionInfo missionInfo, CountryInfo countryInfo) {
    ClientMissionInfo clientMissionInfo = new ClientMissionInfo();
    clientMissionInfo.setId(missionInfo.getId());
    clientMissionInfo.setCode(missionInfo.getCode());
    clientMissionInfo.setCountryName(countryInfo.getCountryName());
    clientMissionInfo.setMissionName(missionInfo.getMissionName());
    clientMissionInfo.setUrls(JSON.parseArray(missionInfo.getUrls()));
    clientMissionInfo.setHtmlStartKeywords(missionInfo.getHtmlStartKeywords());
    clientMissionInfo.setUrlStartKeywords(missionInfo.getUrlStartKeywords());
    clientMissionInfo.setHtmlEndKeywords(missionInfo.getHtmlEndKeywords());
    clientMissionInfo.setUrlEndKeywords(missionInfo.getUrlEndKeywords());
    clientMissionInfo.setTimeOut(missionInfo.getTimeOut());
    clientMissionInfo.setAdvertiserName(missionInfo.getAdvertiserName());
    clientMissionInfo.setAffiliateName(missionInfo.getAffiliateName());
    clientMissionInfo.setIpIntervalType(missionInfo.getIpIntervalType());
    clientMissionInfo.setIpIntervalDays(missionInfo.getIpIntervalTime());
    clientMissionInfo.setCloudStorage(missionInfo.getCloudStorage());
    clientMissionInfo.setMissionRefererConfig(JSON.parseObject(missionInfo.getRefererSourceConfig()));
    clientMissionInfo.setMissionDataConfig(JSON.parseObject(missionInfo.getDataSourceConfig()));
    Map<String, List<ClientScriptFileInfo>> clientListMap = new HashMap<>();
    List<SysFileInfoResponseDTO> sysFileInfos = missionInfo.getFileInfos();
    if(!ObjectUtils.isEmpty(sysFileInfos) && sysFileInfos.size()>0){
      Map<String, List<SysFileInfoResponseDTO>> scriptListMap = sysFileInfos.stream().collect(Collectors.groupingBy(SysFileInfoResponseDTO::getFileTypeCode));
      for (Map.Entry<String, List<SysFileInfoResponseDTO>> item : scriptListMap.entrySet()) {
        if(!ObjectUtils.isEmpty(item) && item.getValue().size()>0){
          List<ClientScriptFileInfo> collect = item.getValue().stream().map(f -> new ClientScriptFileInfo(f)).collect(Collectors.toList());
          clientListMap.put(item.getValue().get(0).getFileTypeName(),collect);
        }

      }
    }

    clientMissionInfo.setScriptFiles(clientListMap);
   // clientMissionInfo.setScriptFile(new ClientScriptFileInfo(missionInfo.getFileInfo()));
    //if (!ObjectUtils.isEmpty(missionInfo.getFileInfo())) {
    //  clientMissionInfo.setScriptFileName(missionInfo.getFileInfo().getShowName());
    //}
    clientMissionInfo.setProxies(ClientProxyInfo.getList(missionInfo.getProxies()));
    clientMissionInfo.setIpFrauds(ClientIpFraudInfo.getList(missionInfo.getIpFrauds()));
    clientMissionInfo.setDeviceFrauds(ClientDeviceFraudInfo.getList(missionInfo.getDeviceFrauds()));
    clientMissionInfo.setPlugins(ClientPluginInfo.getList(missionInfo.getPlugins()));
    clientMissionInfo.setTrafficBypassStatus(missionInfo.getFlowFilterStatus());
    clientMissionInfo.setSslDecrypt(missionInfo.getSslDecrypt());
    clientMissionInfo.setTrafficBypassRules(ClientFlowFilterInfo.getList(missionInfo.getFlowFilterRules()));
    List<ClientEngineInfo> engineInfos = new ArrayList<>();
    if(!ObjectUtils.isEmpty(missionInfo.getEngineInfo())){
      missionInfo.getEngineInfo().forEach(f -> engineInfos.add(new ClientEngineInfo(f)));
    }
    clientMissionInfo.setEngineInfo(engineInfos);
//    clientMissionInfo.setMissionData(missionInfo.getDataSource());
//    clientMissionInfo.setMissionReferer(missionInfo.getRefererSource());
  return clientMissionInfo;
  }
}
